package effective;

import java.util.*;
import java.util.stream.Collectors;
class Indicator {
  class MessageItem implements Comparable<MessageItem> {
    String message;
    long timeStamp;
    MessageItem(String n) {
      timeStamp = System.nanoTime();
      message = n;
    }
    @Override
    public int compareTo(MessageItem o) {
      return Long.compare(this.timeStamp, o.timeStamp);
    }
    @Override
    public String toString() {
      return String.format("%d : %s", timeStamp, message);
    }
  }
  List<MessageItem> messageList;
  String msgPrefix;
  public Indicator(String p) {
    this.messageList = new ArrayList<>();
    msgPrefix = p;
    this.messageList.add(new MessageItem(msgPrefix + ": init"));
  }

  public void setConstructMessage(String mesg) {
    messageList.add(new MessageItem(String.format("[%s] constructing: %s", msgPrefix, mesg)));
  }
  public void setStaticMessage(String mesg) {
    messageList.add(new MessageItem(String.format("[%s] static field: %s", msgPrefix, mesg)));
  }

  public Indicator mergeOther(Indicator ind) {
    Indicator merged = new Indicator("");
    merged.messageList.clear();
    merged.messageList.addAll(this.messageList);
    merged.messageList.addAll(ind.messageList);
    return merged;
  }
  @Override
  public String toString() {
    return this.messageList.stream()
              .sorted()
              .map(Object::toString)
              .collect(Collectors.joining("\n"));
  }
}